package com.qingyun.customview.bezier.path_anim

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathMeasure
import android.graphics.PorterDuff
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.annotation.ColorInt
import androidx.core.graphics.createBitmap
import com.qingyun.customview.R
import kotlin.random.Random

class PathAnimView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : View(context, attrs, defStyleAttr), View.OnClickListener {
    private val mPath: Path
    private val mPathMeasure: PathMeasure
    private var original: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_starts2)
    private var mCurrentStar: Bitmap? = null
    private val mPathPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.STROKE
        isAntiAlias = true
        isDither = true
        strokeWidth = 2f
    }
    private val currentPos: FloatArray = FloatArray(2)
    private val mStarPaint = Paint().apply {
        color = Color.BLUE
        isAntiAlias = true
    }

    private val mColors = intArrayOf(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.CYAN)

    init {
        setOnClickListener(this)
        mPath = createPath()
        mPathMeasure = PathMeasure(mPath, false)
        startAnim()
    }

    private fun drawStar(@ColorInt color: Int): Bitmap {
        val outBitmap = createBitmap(original.width, original.height)
        val canvas = Canvas(outBitmap)
        canvas.drawBitmap(original, 0f, 0f, null)
        canvas.drawColor(color, PorterDuff.Mode.SRC_IN)
        canvas.setBitmap(null)
        return outBitmap
    }

    private fun startAnim() {
        ValueAnimator.ofFloat(0f, mPathMeasure.length).apply {
            duration = 2000
            interpolator = LinearInterpolator()
            repeatCount = ValueAnimator.INFINITE
            addUpdateListener {
                val v = it.animatedValue as Float
                val floats = FloatArray(2)
                mPathMeasure.getPosTan(v, floats, null)
                currentPos[0] = floats[0]
                currentPos[1] = floats[1]
                invalidate()
            }
        }.also {
            it.start()
        }
    }

    private fun createPath(): Path {
        val path = Path()

        path.reset()
        val startX = getScreenWidth() / 2f
        val startY = getScreenHeight().toFloat()
        val endX = getScreenWidth() / 2f
        val endY = 0f

        val pivot1X = getScreenWidth().toFloat()
        val pivot1Y = getScreenHeight() * 3 / 4f
        val pivot2X = 0f
        val pivot2Y = getScreenHeight() / 4f
        // 移动到起始点
        path.moveTo(startX, startY)
        path.cubicTo(pivot1X, pivot1Y, pivot2X, pivot2Y, endX, endY)
        return path
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawPath(mPath, mPathPaint)

        mCurrentStar?.let {
            canvas.drawBitmap(it, currentPos[0], currentPos[1], mStarPaint)
        }
    }

    //计算屏幕宽度
    fun getScreenWidth(): Int {
        return resources.displayMetrics.widthPixels
    }

    //计算屏幕高度
    fun getScreenHeight(): Int {
        return resources.displayMetrics.heightPixels
    }

    override fun onClick(v: View?) {
        val starColor = mColors[Random.nextInt(mColors.size)]
        mCurrentStar = drawStar(starColor)
        startAnim()
    }


}